0

I have a problem with the Label's width. If I change the size of the window and the label's width doesn't fit to the window.

I expect something like:

Initial: 012345678901234567890123456789

After resize: 01234567890123...

But the actual state:

Initial state

After resize:

After resize

How can I get my expected result?

Here is the .fxml file

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.HBox?>
<AnchorPane xmlns="http://javafx.com/javafx"
 xmlns:fx="http://javafx.com/fxml"
 fx:controller="stackoverflow.labeltest.Controller">
 <HBox>
 <Label fx:id="label"/>
 </HBox>
</AnchorPane>
asked May 24, 2018 at 10:09
1
  • 1
    The HBox is probably still the same size, as AnchorPane doesn't resize its children (I think). Try removing the AnchorPane, having the HBox as the root. Commented May 24, 2018 at 10:15

1 Answer 1

1

An AnchorPane does not resize a child without constraints. You need to set the rightAnchor and leftAnchor constraints of the HBox. (You could also simply use the HBox as root.)

<AnchorPane xmlns="http://javafx.com/javafx"
 xmlns:fx="http://javafx.com/fxml"
 fx:controller="stackoverflow.labeltest.Controller">
 <children>
 <HBox AnchorPane.rightAnchor="0" AnchorPane.leftAnchor="0">
 <children>
 <Label fx:id="label"/>
 </children>
 </HBox>
 </children>
</AnchorPane>
answered May 24, 2018 at 10:25
Sign up to request clarification or add additional context in comments.

1 Comment

Yes you are right I am using simply the HBox as root and it works :) Thanks.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.